
***********************
***********************
**Regresiones básicas**
***********************
***********************
clear
use "$base_out/alum_pb_2018_modified"

preserve
use "$base_out/alum_pb_2015_modified", clear
collapse (mean) mat_std leg_std promedio_std prom_pca_std, by( school_id)
rename mat_std mat_std_2015
rename leg_std leg_std_2015
rename promedio_std promedio_std_2015
rename prom_pca_std prom_pca_std_2015
tempfile temp
save `temp'
restore
drop _merge
merge m:1 school_id using `temp'
drop _merge
drop cct_num
gen cct_num=school_id
merge m:1 cct_num using "$base_out/management_summary_2015.dta"

drop _merge

gen NoMissing_DWMS =!missing(index_sted1)
replace index_sted1=0 if NoMissing_DWMS==0

gen NoMissing_mat_std_2015 =!missing(mat_std_2015)
replace mat_std_2015=0 if NoMissing_mat_std_2015==0

gen NoMissing_leg_std_2015 =!missing(leg_std_2015)
replace leg_std_2015=0 if NoMissing_leg_std_2015==0

destring, replace

gen NoMissing_ano_nac =!missing(ano_nac)
replace ano_nac=0 if NoMissing_ano_nac==0

tab genero, gen(D_genero)
gen NoMissing_D_genero1 =!missing(D_genero1)
replace D_genero1=0 if NoMissing_D_genero1==0

tab modalidad, gen(D_modalidad)

global lagged_controles NoMissing_mat_std_2015 c.NoMissing_mat_std_2015#c.mat_std_2015 c.NoMissing_mat_std_2015#c.mat_std_2015#c.mat_std_2015 NoMissing_leg_std_2015 c.NoMissing_leg_std_2015#c.leg_std_2015 c.NoMissing_leg_std_2015#c.leg_std_2015#c.leg_std_2015
global lagged_DWMS NoMissing_DWMS c.NoMissing_DWMS#c.index_sted1
global student_controles NoMissing_ano_nac c.NoMissing_ano_nac#c.ano_nac NoMissing_ano_nac c.NoMissing_ano_nac#c.D_genero1  c.NoMissing_ano_nac#c.NoMissing_D_genero1#c.ano_nac#c.D_genero1
global school_controles m_alta m_media m_baja urbana lage


***PEC PLUS VS. PEC****
label var PEC_Plus " "
label var PEC " "

eststo clear
eststo: reghdfe prom_pca_std  PEC_Plus  if (treatgroup2!="Control" & state!=10) , cluster(cct_num) a(estrato)
eststo: reghdfe prom_pca_std $lagged_controles PEC_Plus  if (treatgroup2!="Control" & state!=10) , cluster(cct_num) a(estrato)
eststo: reghdfe prom_pca_std $lagged_controles $lagged_DWMS PEC_Plus  if (treatgroup2!="Control" & state!=10) , cluster(cct_num) a(estrato)
eststo: reghdfe prom_pca_std $lagged_controles $lagged_DWMS $student_controles PEC_Plus  if (treatgroup2!="Control" & state!=10) , cluster(cct_num) a(estrato)


matrix tempm=e(b)
local tempm=string(tempm[1,1], "%9.2fc")
local tempm_effect=tempm[1,1]
file open newfile using "$tables/prom_pca_std_coef_PEC_PLUS_vs_PEC_robust.tex", write replace
file write newfile "`tempm'"
file close newfile
test PEC_Plus
if r(p)<0.001 {
	di "peque"
	local tempm ="$<0.001$"
	file open newfile using "$tables/prom_pca_std_pvalue_PEC_PLUS_vs_PEC_robust.tex", write replace
	file write newfile "`tempm'"
	file close newfile
}
if r(p)>0.001 {
	di "grande"
	local tempm=string(r(p), "%9.2fc")
	file open newfile using "$tables/prom_pca_std_pvalue_PEC_PLUS_vs_PEC_robust.tex", write replace
	file write newfile "`tempm'"
	file close newfile
}
*matrix b_`x'=e(b)
*estimates store m1`x'

lincom PEC_Plus
matrix tempm=r(estimate)+invttail(r(df),0.025)*r(se)
local tempm=string(tempm[1,1], "%9.2fc")

file open newfile using "$tables/prom_pca_std_RuleOut_PEC_PLUS_vs_PEC_robust.tex", write replace
file write newfile "`tempm'"
file close newfile


esttab using "$tables/Resultados_PEC_PLUSVSPECESTR_Robust.tex" ,  fragment se ar2  label b(a2) se(a2) nocon nonumber nomtitles nolines nogaps /// 
replace collabels(none)  ///
keep(PEC_Plus) stats(N , fmt("%9.2gc") labels("No. of obs." )) star(* 0.10 ** 0.05 *** 0.01)
